package com.huawei.hms.framework.network.grs.b;

import android.text.TextUtils;
import com.huawei.hms.api.ConnectionResult;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.common.StringUtils;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONObject;

public class c {
    private static final String a = c.class.getSimpleName();
    private Map<String, List<String>> b;
    private byte[] c;
    private int d = 0;
    private long e;
    private String f;
    private int g = 2;
    private int h = ConnectionResult.RESOLUTION_REQUIRED;
    private String i = "";
    private String j = "";
    private String k = "";
    private Exception l;
    private String m;
    private int n;

    public c(int i, Map<String, List<String>> map, byte[] bArr, long j) {
        this.d = i;
        this.b = map;
        this.c = bArr;
        this.e = j;
        j();
    }

    public c(Exception exception, long j) {
        this.l = exception;
        this.e = j;
    }

    private void a(Map<String, String> map) {
        long parseLong;
        Throwable e;
        if (map == null || map.size() <= 0) {
            Logger.w(a, "getExpireTime {headers == null} or {headers.size() <= 0}");
            return;
        }
        String str;
        if (map.containsKey("Cache-Control")) {
            str = (String) map.get("Cache-Control");
            if (str.contains("max-age=")) {
                try {
                    parseLong = Long.parseLong(str.substring(str.indexOf("max-age=") + "max-age=".length()));
                    try {
                        Logger.v(a, "Cache-Control value{%s}", Long.valueOf(parseLong));
                    } catch (NumberFormatException e2) {
                        e = e2;
                        Logger.w(a, "getExpireTime addHeadersToResult NumberFormatException", e);
                        parseLong = 86400;
                        Logger.v(a, "convert expireTime{%s}", Long.valueOf(parseLong * 1000));
                        d(String.valueOf(parseLong + System.currentTimeMillis()));
                    }
                } catch (Throwable e3) {
                    e = e3;
                    parseLong = 0;
                    Logger.w(a, "getExpireTime addHeadersToResult NumberFormatException", e);
                    parseLong = 86400;
                    Logger.v(a, "convert expireTime{%s}", Long.valueOf(parseLong * 1000));
                    d(String.valueOf(parseLong + System.currentTimeMillis()));
                }
            }
            parseLong = 0;
        } else if (map.containsKey("Expires")) {
            Logger.v(a, "expires is{%s}", (String) map.get("Expires"));
            DateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.ROOT);
            Object obj = null;
            if (map.containsKey("Date")) {
                obj = (String) map.get("Date");
                Logger.v(a, "dateStr{%s}", obj);
            }
            try {
                parseLong = (simpleDateFormat.parse(str).getTime() - (TextUtils.isEmpty(obj) ? new Date() : simpleDateFormat.parse(obj)).getTime()) / 1000;
            } catch (Throwable e32) {
                Logger.w(a, "getExpireTime ParseException.", e32);
            }
        } else {
            Logger.i(a, "response headers neither contains Cache-Control nor Expires.");
            parseLong = 0;
        }
        if (parseLong <= 0 || parseLong > 2592000) {
            parseLong = 86400;
        }
        Logger.v(a, "convert expireTime{%s}", Long.valueOf(parseLong * 1000));
        d(String.valueOf(parseLong + System.currentTimeMillis()));
    }

    private void b(int i) {
        this.g = i;
    }

    private void b(String str) {
        this.j = str;
    }

    private void c(int i) {
        this.h = i;
    }

    private void c(String str) {
        this.f = str;
    }

    private void d(String str) {
        this.k = str;
    }

    private void e(String str) {
        this.i = str;
    }

    private void j() {
        k();
        m();
    }

    private void k() {
        if (e()) {
            try {
                a(l());
            } catch (Throwable e) {
                Logger.w(a, "parseHeader catch JSONException", e);
            }
        }
    }

    private Map<String, String> l() {
        Map<String, String> hashMap = new HashMap(16);
        if (this.b == null || this.b.size() <= 0) {
            Logger.v(a, "parseRespHeaders {respHeaders == null} or {respHeaders.size() <= 0}");
            return hashMap;
        }
        for (Entry entry : this.b.entrySet()) {
            hashMap.put((String) entry.getKey(), ((List) entry.getValue()).get(0));
        }
        return hashMap;
    }

    private void m() {
        if (e()) {
            try {
                String byte2Str = StringUtils.byte2Str(this.c);
                JSONObject jSONObject = new JSONObject(byte2Str);
                int i = jSONObject.getInt(HwAccountConstants.SERVICETOKENAUTH_IS_SECCUSS);
                b(i);
                Object obj = (i != 0 || byte2Str.indexOf("services") == -1) ? null : 1;
                if (i == 1 || obj != null) {
                    c(jSONObject.getJSONObject("services").toString());
                    if (obj != null) {
                        b(jSONObject.getString("errorList"));
                        return;
                    }
                    return;
                }
                b(2);
                c(jSONObject.getInt("errorCode"));
                e(jSONObject.getString("errorDesc"));
                return;
            } catch (Throwable e) {
                Logger.w(a, "GrsResponse GrsResponse(String result) JSONException", e);
                b(2);
                return;
            }
        }
        b(2);
    }

    public String a() {
        return this.f;
    }

    public void a(int i) {
        this.n = i;
    }

    public void a(String str) {
        this.m = str;
    }

    public String b() {
        return this.k;
    }

    public int c() {
        return this.g;
    }

    public int d() {
        return this.h;
    }

    public boolean e() {
        Logger.v(a, "GrsResponse return http code:%s", Integer.valueOf(this.d));
        return this.d == 200;
    }

    public Exception f() {
        return this.l;
    }

    public String g() {
        return this.m;
    }

    public int h() {
        return this.d;
    }

    public long i() {
        return this.e;
    }
}
